package cn.ainit.bj.isbn;

import java.sql.CallableStatement;
import java.sql.Connection;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ainit.bj.isbn.entity.ISBNApply;
import cn.ainit.sys.common.dao.BusinessBaseDao;
@Transactional
@Repository("isbnApplyDao")
public class ISBNApplyDaoImpl extends BusinessBaseDao implements ISBNApplyDao {

	@Override
	public boolean saveISBNApply(ISBNApply isbnApply) throws Exception {
		try {
			this.getSessionFactory().getCurrentSession().save(isbnApply);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		return true;
	}

	@Override
	public ISBNApply toUpdateISBN(String isbnId) throws Exception {
		return (ISBNApply) this.getSessionFactory().getCurrentSession().get(ISBNApply.class, isbnId);
	}

	@Override
	public boolean updISBNApply(ISBNApply isbnApply) throws Exception {
		this.getSessionFactory().getCurrentSession().update(isbnApply);
		return true;
	}
	@Override
	public String execISBNApply(String isbnId) throws Exception {
		String putValue="";
		Connection conn = null;
		CallableStatement cs = null;
		try {
			 conn= this.getJdbcTemplate().getDataSource().getConnection();
			 String sql = "{Call PK_BJ_XT.up_IsbnToProduct(?,?)}";
			 cs = conn.prepareCall(sql);
			 cs.setString(1,isbnId);
			 cs.registerOutParameter(2,oracle.jdbc.OracleTypes.VARCHAR);
			 cs.execute();
			 putValue = cs.getString(2);
		} catch (Exception e) {
			String msg = e.getMessage();
			msg = msg.substring(10,msg.length());
			msg = msg.substring(0,msg.indexOf("ORA"));
			throw new RuntimeException(msg);
		}finally{
			if(cs!=null){
				cs.close();
			}
			if(conn!=null){
				conn.close();
			}
		}
		return putValue;
	}

	@Override
	public String updISBNVerify(String sql) throws Exception {
		return this.getPkStr(sql);
	}
	@Override
	public boolean execSql(String sql) throws Exception {
		return this.executeSQL(sql);
	}
	@Override
	public int execSqlApply(String sql) throws Exception {
		return this.getJdbcTemplate().queryForInt(sql);
	}
}
